package jp.co.axisware.taskboard.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import jp.co.axisware.taskboard.client.managed.request.ApplicationRequestFactory;
import jp.co.axisware.taskboard.client.managed.request.ParticipationProxy;
import jp.co.axisware.taskboard.client.managed.request.ParticipationRequest;
import jp.co.axisware.taskboard.client.managed.ui.ParticipationDetailsView;
import jp.co.axisware.taskboard.client.managed.ui.ParticipationEditView;
import jp.co.axisware.taskboard.client.managed.ui.ParticipationListView;
import jp.co.axisware.taskboard.client.managed.ui.ParticipationMobileDetailsView;
import jp.co.axisware.taskboard.client.managed.ui.ParticipationMobileEditView;
import jp.co.axisware.taskboard.client.scaffold.ScaffoldApp;
import jp.co.axisware.taskboard.client.scaffold.place.CreateAndEditProxy;
import jp.co.axisware.taskboard.client.scaffold.place.FindAndEditProxy;
import jp.co.axisware.taskboard.client.scaffold.place.ProxyPlace;

public class ParticipationActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public ParticipationActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new ParticipationDetailsActivity((EntityProxyId<ParticipationProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? ParticipationMobileDetailsView.instance() : ParticipationDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<jp.co.axisware.taskboard.client.managed.request.ParticipationProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<ParticipationProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        ParticipationEditView.instance().setCreating(true);
        final ParticipationRequest request = requests.participationRequest();
        Activity activity = new CreateAndEditProxy<ParticipationProxy>(ParticipationProxy.class, request, ScaffoldApp.isMobile() ? ParticipationMobileEditView.instance() : ParticipationEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ParticipationProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new ParticipationEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ParticipationMobileEditView.instance() : ParticipationEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        ParticipationEditView.instance().setCreating(false);
        EntityProxyId<ParticipationProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<ParticipationProxy>(proxyId, requests, ScaffoldApp.isMobile() ? ParticipationMobileEditView.instance() : ParticipationEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ParticipationProxy proxy) {
                ParticipationRequest request = requests.participationRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new ParticipationEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ParticipationMobileEditView.instance() : ParticipationEditView.instance(), activity, proxyId);
    }
}
